import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

import Home from '@/pages/Home'
import Register from '@/pages/Register'
import Login from '@/pages/Login'
import Search from '@/pages/Search'
import Detail from '@/pages/Detail'
import AddCartSuccess from '@/pages/AddCartSuccess'
import ShopCart from '@/pages/ShopCart'
import store from '@/store'
import Trade from '@/pages/Trade'
import Pay from '@/pages/Pay'
import PaySuccess from '@/pages/PaySuccess'
import Center from '@/pages/Center'
import MyOrder from '@/pages/Center/MyOrder'
import Abulkorder from '@/pages/Center/Abulkorder'

let origunPush = VueRouter.prototype.push;
let origunReplace = VueRouter.prototype.replace;


VueRouter.prototype.push = function (location, resolve, reject) {
  if (resolve && reject) {
    origunPush.call(this, location, resolve, reject)
  } else {
    origunPush.call(this, location, () => { }, () => { })
  }
}

VueRouter.prototype.replace = function (location, resolve, reject) {
  if (resolve && reject) {
    origunReplace.call(this, location, resolve, reject)
  } else {
    origunReplace.call(this, location, () => { }, () => { })
  }
}

let router = new VueRouter({
  mode: 'hash',
  routes: [
    {
      path: '/center',
      name: 'Center',
      component: () => import('@/pages/Center'),
      meta: { show: true },
      children: [
        {
          path: 'myorder',
          component: MyOrder
        },
        {
          path: 'Abulkorder',
          component: Abulkorder
        },
        {
          path: '/center',
          redirect: '/center/myorder'
        }
      ]
    },
    {
      path: '/paysuccess',
      name: 'PaySuccess',
      component: () => import('@/pages/PaySuccess'),
      meta: { show: true }
    },
    {
      path: '/pay',
      name: 'Pay',
      component: () => import('@/pages/Pay'),
      meta: { show: true },
      beforeEnter: (to, from, next) => {
        if (from.path == '/trade') {
          next()
        } else {
          next(false)
        }
      }

    },
    {
      path: '/trade',
      name: 'Trade',
      component: () => import('@/pages/Trade'),
      meta: { show: true },
      beforeEnter: (to, from, next) => {
        if (from.path == '/shopcart' || '/login') {
          next()
        } else {
          next(false)
        }
      }
    },
    {
      path: '/shopcart',
      name: 'ShopCart',
      component: () => import('@/pages/ShopCart'),
      meta: { show: true }
    },
    {
      path: '/addcartsuccess',
      name: 'addcartsuccess',
      component: () => import('@/pages/AddCartSuccess'),
      meta: { show: true }
    },
    {
      path: '/detail/:skuid?',
      component: () => import('@/pages/Detail'),
      meta: { show: true }
    },
    {
      path: '/home',
      component: () => import('@/pages/Home'),
      meta: { show: true }
    },
    {
      path: '/register/',
      component: () => import('@/pages/Register'),
      meta: { show: false }

    },
    {
      path: '/login',
      component: () => import('@/pages/Login'),
      meta: { show: false }
    },
    {
      path: '/search/:keyword?',
      component: () => import('@/pages/Search'),
      meta: { show: true },
      name: 'search',

    },
    {
      path: '*',
      redirect: '/home'
    }
  ],
  scrollBehavior(to, from, savedPosition) {
    return { y: 0 }
  }
})

router.beforeEach(async (to, from, next) => {

  let token = store.state.user.token
  let name = store.state.user.userInfo.name
  if (token) {
    if (to.path == '/login') {
      next('/home')
    } else {
      if (name) {
        next()
      } else {
        try {
          await store.dispatch('user/getUserInfo')
          next()
        } catch (error) {
          store.dispatch('user/userLogout')
          next('/login')
        }
      }
    }
  } else {
    let toPath = to.path
    if (toPath.indexOf('trade') != -1 || toPath.indexOf('/pay') != -1 || toPath.indexOf('/center') != -1) {
      next('/login?redirect=' + toPath)
    } else {
      next()
    }
  }
})

export default router